package com.customview.customPath;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.customview.R;

public class CustomPathPic extends View {

    Bitmap bitmap;
    Paint mPaint;

    public CustomPathPic(Context context) {
        this(context, null);
    }

    public CustomPathPic(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomPathPic(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);


    }




    int x=10;
    int y=10;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.GRAY);

        test5();
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);


        //实例化 一个矩阵对象
        Matrix matrix =new Matrix();
        matrix.setScale(1f,-1f);

        //产生和原图一样大小的倒影图

        Bitmap tmp= Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);

        //绘制原图
        canvas.drawBitmap(bitmap,x,y,null);

        //保存图层
        int src=canvas.saveLayer(x,y,x+bitmap.getWidth(),y+bitmap.getHeight()*2,null,Canvas.ALL_SAVE_FLAG);

        //绘制倒影图片
        canvas.drawBitmap(tmp, x, y + bitmap.getHeight(), null);

        //设置混淆效果
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mPaint.setShader(new LinearGradient(x,y+bitmap.getHeight(),x,y+bitmap.getHeight()+bitmap.getHeight()/4,Color.BLACK,
                Color.TRANSPARENT,Shader.TileMode.CLAMP));
       canvas.drawBitmap(tmp,x,y+bitmap.getHeight(),mPaint);

       canvas.drawRect(x,y+bitmap.getHeight(),x+bitmap.getWidth(),y+bitmap.getHeight()*2,mPaint);

       mPaint.setXfermode(null);
       canvas.restoreToCount(src);
    }

    private void test5() {


    }
}
